/*
 * Created on Jul 10, 2006
 * 
 */

package com.probiz.estore.core.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.Assert;

/**
 * @author Legend
 * 
 */
public final class UrlUtil {
	public final static String appendParamToUrl(final String url,
			final String paramName, final String paramValue) {
		Assert.hasText(paramName);
		Assert.hasText(paramValue);

		StringBuilder sb = new StringBuilder(url);
		if (url.indexOf("?") == -1) {
			sb.append("?");
		} else if (!url.endsWith("&")) {
			sb.append("&");
		}
		sb.append(paramName).append("=").append(paramValue);
		return sb.toString();
	}

	private static String buildFullRequestUrl(String scheme, String serverName,
			int serverPort, String contextPath, String requestUrl,
			String servletPath, String requestURI, String pathInfo,
			String queryString) {
		boolean includePort = true;
		if ("http".equals(scheme.toLowerCase()) && (serverPort == 80)) {
			includePort = false;
		}
		if ("https".equals(scheme.toLowerCase()) && (serverPort == 443)) {
			includePort = false;
		}
		return scheme
				+ "://"
				+ serverName
				+ ((includePort) ? (":" + serverPort) : "")
				+ contextPath
				+ buildRequestUrl(servletPath, requestURI, contextPath,
						pathInfo, queryString);
	}

	private static String buildRequestUrl(String servletPath,
			String requestURI, String contextPath, String pathInfo,
			String queryString) {
		String uri = servletPath;

		if (uri == null) {
			uri = requestURI;
			uri = uri.substring(contextPath.length());
		}
		return uri + ((pathInfo == null) ? "" : pathInfo)
				+ ((queryString == null) ? "" : ("?" + queryString));
	}

	public static String formatUrl(String url) {
		return formatUrl(new StringBuilder(url)).toString();
	}

	public static StringBuilder formatUrl(StringBuilder url) {
		int idx = 0;
		while ((idx = url.indexOf("//")) >= 0) {
			url.deleteCharAt(idx);
		}
		// idx = url.length()-1;
		// if (url.charAt(idx)=='/') {
		// url.deleteCharAt(idx);
		// }
		return url;
	}

	public static String getDomainPath(HttpServletRequest request) {
		String fullDomain = request.getScheme() + "://"
				+ request.getHeader("host") + request.getContextPath();
		return fullDomain;
	}

	/**
	 * 取到当前请求的全路径
	 * 
	 * @param r
	 * @return
	 */
	public static String getFullRequestUrl(HttpServletRequest r) {
		return buildFullRequestUrl(r.getScheme(), r.getServerName(), r
				.getServerPort(), r.getContextPath(), r.getRequestURL()
				.toString(), r.getServletPath(), r.getRequestURI(), r
				.getPathInfo(), r.getQueryString());
	}

	/**
	 * reference getParamFromUrl(String uri,String paramName)
	 * 
	 * @param request
	 * @param paramName
	 * @return
	 */
	public final static String getParamFromUrl(
			final HttpServletRequest request, String paramName) {
		return getParamFromUrl(request.getRequestURI(), paramName);
	}

	/**
	 * only for *.html Sample:uri="/index=path1.2.3=page4.html";
	 * getParamFromUrl(uri,"path") will returns "1.2.3";
	 * getParamFromUrl(uri,"page") will returns "4";
	 * 
	 * @param uri
	 * @param paramName
	 * @return
	 */
	public final static String getParamFromUrl(String uri, String paramName) {
		int indexStart = uri.indexOf("=" + paramName);
		if (indexStart == -1) {
			return null;
		}
		String rs = uri.substring(indexStart + paramName.length() + 1);
		int indexEnd = rs.indexOf("=");
		if (indexEnd == -1) {
			if (uri.contains("_dy")) {
				indexEnd = rs.indexOf("_dy.html");
			} else {
				indexEnd = rs.indexOf(".html");
			}
		}
		if (indexEnd == -1) {
			return null;
		}
		rs = rs.substring(0, indexEnd);
		return rs;
	}

	public static String getParentUrl(final String url) {
		int idx = 0;
		while (true) {
			int nextIdx = url.indexOf("/", idx + 1);
			if (nextIdx > idx) {
				idx = nextIdx;
			} else {
				break;
			}
		}
		return url.substring(0, idx) + "/index.html";
	}

	public static String getRequestServletPath(HttpServletRequest r) {
		return buildRequestUrl(r.getServletPath(), r.getRequestURI(), r
				.getContextPath(), r.getPathInfo(), r.getQueryString());
	}

	public final static String getUrlFromRequest(HttpServletRequest request) {
		String url = request.getServletPath();
		Enumeration e = request.getParameterNames();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String value = request.getParameter(name);
			url = appendParamToUrl(url, name, value);
		}
		return url;
	}

	public final static boolean isUrlValid(String strUrl) {
		try {
			new URL(strUrl);
			return true;
		} catch (MalformedURLException e) {
			return false;
		}

	}

	public static void main(String[] args) {
		System.out
				.println(UrlUtil
						.replaceParamInUrl(
								"http://localhost:8080/StoreAdmin/system/systemLanguage.html?systemLanguageId=82&doAction=edit&from=list",
								"systemLanguageId", "1234"));
	}

	public static String parsePageName(final String url) {
		int idx2 = url.lastIndexOf(".");
		if (idx2 > 0) {// request a specified page
			return url.substring(url.lastIndexOf('/') + 1, idx2);
		} else {
			return "Page_name_cannot_be_parsed.";
		}
	}

	public final static String removeParamFromUrl(final String url,
			final String paramName) {
		Assert.hasText(paramName);

		StringBuilder sb = new StringBuilder(url);
		int idx = sb.indexOf("?");
		if (idx > 0) {
			int idx1 = sb.indexOf(paramName, idx);
			if (idx1 > 0) {
				int idx2 = sb.indexOf("&", idx1);
				if (idx2 == -1) {
					idx2 = sb.length();
				} else {
					idx2++;
				}
				return sb.delete(idx1, idx2).toString();
			}
		}

		return url;
	}

	public final static String replaceParamInUrl(final String url,
			final String paramName, final String paramValue) {
		return appendParamToUrl(removeParamFromUrl(url, paramName), paramName,
				paramValue);
	}

	private UrlUtil() {
	}
}
